package com.woniu.p2p.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.*;
import com.woniu.p2p.mapper.*;
import com.woniu.p2p.service.P2PIndexService;
import com.woniu.p2p.util.DateUntil;
import com.woniu.p2p.vo.MonthAndWeekUserInvestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class P2PIndexServiceImpl implements P2PIndexService {
    @Autowired
    HomepageMapper homepageMapper;
    @Autowired
    ShowMapper showMapper;
    @Autowired
    InvestMapper investMapper;
    @Autowired
    NewMapper newMapper;
    @Autowired
    UserInvestMapper userInvestMapper;

    @Override
    public Homepage findAllHomepage(Integer id) {
        return homepageMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Show> findAllShow(Integer start, Integer num) {
        PageHelper.startPage(start,num);
        List<Show> allShowNew = showMapper.findAllShowNew();
        PageInfo<Show> pageInfo = new PageInfo<>(allShowNew);
        return pageInfo;
    }

    @Override
    public PageInfo<Invest> findAllInvestNewThree(Integer start, Integer num) {
        PageHelper.startPage(start,num);
        List<Invest> allInvestNewThree = investMapper.findAllInvestNewThree();
        PageInfo<Invest> pageInfo=new PageInfo<>(allInvestNewThree);
        return pageInfo;
    }

    @Override
    public PageInfo<Invest> findAllInvestXin(Integer start, Integer num) {
        PageHelper.startPage(start,num);
        List<Invest> allInvestXin = investMapper.findAllInvestXin();
        PageInfo<Invest> pageInfo=new PageInfo<>(allInvestXin);
        return pageInfo;
    }

    @Override
    public PageInfo<New> findAllNew(Integer start, Integer num) {
        PageHelper.startPage(start,num);
        List<New> news = newMapper.selectAll();
        PageInfo<New> pageInfo=new PageInfo<>(news);
        return pageInfo;
    }

    @Override
    public PageInfo<MonthAndWeekUserInvestVo> findAllUserInvestByMonth(Integer start, Integer num) {
        PageHelper.startPage(start,num);
        Date date = new Date();
        String pastDate = DateUntil.getPastDate(30, date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse=null;
        try {
            parse = sdf.parse(pastDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date date1 = new java.sql.Date(date.getTime());
        java.sql.Date date2 = new java.sql.Date(parse.getTime());
        List<MonthAndWeekUserInvestVo> allByMonth = userInvestMapper.findAllByMonth(date2, date1);
        PageInfo<MonthAndWeekUserInvestVo> pageInfo=new PageInfo<>(allByMonth);
        return pageInfo;
    }

    @Override
    public PageInfo<MonthAndWeekUserInvestVo> findAllUserInvestByWeek(Integer start, Integer num) {
        PageHelper.startPage(start,num);
        Date date = new Date();
        String pastDate = DateUntil.getPastDate(7, date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(pastDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date date1 = new java.sql.Date(date.getTime());
        java.sql.Date date2 = new java.sql.Date(parse.getTime());
        List<MonthAndWeekUserInvestVo> allByMonth = userInvestMapper.findAllByMonth(date2, date1);
        PageInfo<MonthAndWeekUserInvestVo> pageInfo=new PageInfo<>(allByMonth);
        return pageInfo;
    }

    @Override
    public PageInfo<MonthAndWeekUserInvestVo> findAllUserInvest(Integer start, Integer num) {
        PageHelper.startPage(start,num);
        List<MonthAndWeekUserInvestVo> allForZong = userInvestMapper.findAllForZong();
        PageInfo<MonthAndWeekUserInvestVo> pageInfo=new PageInfo<>(allForZong);
        return pageInfo;
    }
}
